function messageGrid()
{
    $('#messageGrid').jqGrid({
                url: siteUrl + 'message/get_messages',
                mtype: 'post',
                datatype: 'json',
                colNames: ['ID', 'Subject',/*'Message', */'Operations'],
                colModel: [
                {name: 'message_id', index: 'information_id', hidden: true, key: true},
                {name: 'subject', index: 'subject'},
//                {name: 'message', index: 'message'},
                {name: 'ops', index: 'ops', align: 'center', sortable: false, formatter: cmsOperations}
                ],


            autowidth: true,
            height: '200',
            pager: $('#pagination'),
            sortname: 'message_id',
            sortorder: 'asc',
            viewrecords: true,
            rownumbers: true,
            ondblClickRow: function(rowId, iRow, iCol, e) {
                window.location = siteUrl + 'message/view/' + rowId;
            }
            ,loadComplete:(function(xhr, status, error){
               var mode = $('#msg').text();
                
                if (  (mode == 'Inbox') || (mode == 'Sent Items')){
                  $('a[name="edit_msg"]').hide(); 
                  $('a[name="del_perm"]').hide();
                } 
                
                if (  (mode == 'Drafts') ){
                  $('a[name="del_perm"]').hide();
                } 
                
                
                
                if (  (mode == 'Trash') ) {
                    $('a[name="del_msg"]').hide();
                    $('a[name="edit_msg"]').hide();
                    $('a[name="del_perm"]').show();
                }

            })
    });
}
function cmsOperations(cell, opts, rowObj)
{
    var id = opts.rowId;
    
    var edit = '<a name="edit_msg" href="' + siteUrl + 'message/edit/' + id + '" title="Update" class="icon icon-update"></a>';

    var del = '<a name="del_msg" href="javascript:$.confirmDelete(' + id + ', \'deleteMessage\', \'Message - Delete Message\')" title="Delete" class="icon icon-delete"></a>';
    
    var del_perm = '<a name="del_perm" href="javascript:$.confirmDelete(' + id + ', \'deleteMessagePerm\', \'Message - Delete Message\')" title="Delete" class="icon icon-delete"></a>';
    
    return edit + del + del_perm;

}

function messageRules()
{



    $('#message_form').validate({
        debug: true,
        rules: {
            subject: 'required',
            recipient: 'required',
            message: {
                required: true
            }

        },
        messages: {
            

        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            saveContent();
        }
    });

}

function saveContent(){
    $.loading('Message - Sending');
    
    $.ajax({
        url: siteUrl + 'message/submit',
        type: 'post',
        data: $('#message_form').serialize(),
        success: function(response) {

            if (response)
            {;
                window.location = siteUrl + 'message';
            }
        }
    });
}

function deleteMessage(messageId)
{
    $.loading('Message - Deleting Record');

    $.ajax({
        url: siteUrl + 'message/delete/' + messageId,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'message';
            }

        }
    });
}


function deleteMessagePerm(messageId)
{
    $.loading('Message - Deleting Record');

    $.ajax({
        url: siteUrl + 'message/delete_perm/' + messageId,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'message';
            }

        }
    });
}

function saveDraft(){
    
    $.loading('Message - Saving Record');

    $.ajax({
        url: siteUrl + 'message/save_as_draft',
        type: 'post',
        dataType: 'json',
        data: $('#message_form').serialize(),
        success: function(response) {

            if (response == true)
            {
                window.location = siteUrl + 'message';
            }

        }
    });
    
    
}
